<?php
/**********************************************************************************
 
 	Functions that aid in creating arrays with dates as keys.
	
	Copyright (C) 2010 Benjamin Thomas
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 	Author: Benjamin Thomas
	Email: thomas.ben@gmail.com
 	Created: 05.21.10
 	Revised: 
 
 	Calls: none
 
 ***********************************************************************************
 */
 
require_once(dirname(__FILE__) . "/../config.php");
date_default_timezone_set(Vars::$locale);

function getKeyDate($time) {
	return date("n/j", $time);
}

function getOffset() {
	$off = substr(date("P", time()),1);
	$off = explode(":", $off);
	return ($off[0] * 60 * 60) + ($off[1] * 60);
}

function createDate($daysAgo) {
	$day = date("j", time()); $month = date("n", time()); $year = date("Y", time());
	return mktime(0,0,0,$month, $day - $daysAgo, $year);
}

function createDailyArray($len, $str = "n/j") {
	//legacy
	if ($str == 0) { $str = "n/j"; }
	//define the keys here
	$arr = array();
	$day = date("j", time()); $month = date("n", time()); $year = date("Y", time());
	for ($i = 0; $i <= $len; $i++) {
		$key = date($str, mktime(0,0,0, $month, $day - $i, $year));
		$arr[$key] = $default;
	}
	$arr = array_reverse($arr);
	
	return $arr;
}

function createTimestampArray($len, $default = 0) {
	$arr = array();
	$len--;
	$day = date("j", time()); $month = date("n", time()); $year = date("Y", time());
	for ($i = 0; $i <= $len; $i++) {
		$key = mktime(0,0,0, $month, $day - $i, $year) * 1000;
		$arr[] = array($key, $default);
	}
	$arr = array_reverse($arr);
	
	return $arr;
}

function getTime() { 
    $timer = explode( ' ', microtime() ); 
    $timer = $timer[1] + $timer[0]; 
    return $timer; 
}

//Not a time function, but no math library and I'm too lazy to write the includes
function safediv($num, $denom) {
	if ($denom == 0) { return 0; }
	return $num / $denom;
}


?>